package edu.zut.cs.software.example.group.web.spring.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import edu.zut.cs.software.example.QueryInfo.QueryInfo;
import edu.zut.cs.software.example.group.dao.GroupDao;
import edu.zut.cs.software.example.group.domain.Group;
import edu.zut.cs.software.example.group.service.GroupManager;

/**
 * 
 * @author super
 *
 */


@RestController
public class GroupController {

	
	@Autowired 
	GroupDao groupDao;
	 
	
	@Autowired 
	GroupManager groupManager;
	 
	@RequestMapping("/groups")
	public String getAllGroups() {
		System.out.println("访问成功");
		HashMap<String, Object> data = new HashMap<>();		
		List<Group> groups = groupDao.getGroups();
		System.out.println("groups:"+groups);
		if(groups!=null) {
			data.put("groups", groups); 
			data.put("flag", 200);} 
		else {
			data.put("flag",404);}
		String g=JSON.toJSONString(data);
		return g;
		/*HashMap<String, Object> data = groupManager.getGroupsService();
		if(data!=null) {
			data.put("flag", 200);} 
        System.out.println("groups:"+data);
		return JSON.toJSONString(data);*/
	}
	
	@RequestMapping("/addgroup")
	public String addgroup(@RequestBody Group group) {
		//System.out.println("访问成功");
		System.out.println("group:"+group);
		Group addGroup = groupDao.save(group);
		return "ok";	
	}
	
	@RequestMapping("/serchByName")
	public String searchByName(QueryInfo queryInfo) {
		System.out.println("访问成功！");
	    System.out.println("查询数据： "+queryInfo);
	    HashMap<String, Object> res = groupManager.searchByNameService(queryInfo);
        System.out.println("查询结果"+res);
		return JSON.toJSONString(res);
		/*List<Group> group1= groupDao.searchByName(queryInfo.getQuery());
		System.out.println("查询结果列表："+group1);
		HashMap<String,Object> res=new HashMap<>();
		res.put("data", group1);
		String res_string=JSON.toJSONString(res);
		return res_string;*/	
	}
	

}
